//
//  RankViewController.m
//  MusicPlayer
//
//  Created by dlios on 15-5-21.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "RankViewController.h"
#import "SANetWorkingTask.h"
#import "RankTableViewCell.h"
#import <UIImageView+WebCache.h>
#import "RankModel.h"
#import "RankDetailViewController.h"
@interface RankViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, retain) UITableView *table;


@end

@implementation RankViewController
- (void)dealloc
{
    [_table release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.array = [NSMutableArray array];
        self.view.backgroundColor = [UIColor clearColor];
    
    }
    return self;
}








- (void)loadView
{
    [super loadView];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatView];
    
    
    [SANetWorkingTask request:@"http://v1.ard.tj.itlily.com/ttpod?id=281&bundle_id=com.ttpod.music" parmater:nil block:^(NSDictionary *result) {
        NSArray *array = [result objectForKey:@"data"];
        
        for (NSDictionary *rank in array) {
            RankModel *rankModel = [[RankModel alloc] init];
            [rankModel setValuesForKeysWithDictionary:rank];
            [self.array addObject:rankModel];

        }
   
        [_table reloadData];
    }];
    
    
}


//创建tableView

- (void)creatView
{
     self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - self.view.frame.size.height/667 *64 - self.view.frame.size.height/667 *49 - self.view.frame.size.height/667 *70) style:UITableViewStylePlain];
    _table.backgroundColor = [UIColor clearColor];
   
    _table.rowHeight =self.view.frame.size.height/667 *  130;
    _table.delegate = self;
    _table.dataSource = self;
    [self.view addSubview:_table];
    [_table release];

}
// tableView协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentify = @"cell";
    RankTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if (!cell) {
        cell = [[[RankTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentify] autorelease
        ];
    }
    cell.backgroundColor = [UIColor clearColor];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    RankModel *rankModel = [self.array objectAtIndex:indexPath.row];
    cell.titleLabel.text = rankModel.title;
    
    [cell.showView sd_setImageWithURL:[NSURL URLWithString:rankModel.pic_url] placeholderImage:nil];
    
    [cell.numView1 setImage:[UIImage imageNamed:@"bullet-1.png"]];
    [cell.numView2 setImage:[UIImage imageNamed:@"bullet-2.png"]];
    [cell.numView3 setImage:[UIImage imageNamed:@"bullet-3.png"]];
    
    NSString *singer1 = [[rankModel.songlist objectAtIndex:0] objectForKey:@"singerName"];
    NSString *song1 = [[rankModel.songlist objectAtIndex:0] objectForKey:@"songName"];
    NSString *str1 = [NSString stringWithFormat:@"%@ - %@",singer1, song1];
    cell.numLabel1.text = str1;
    
    
    NSString *singer2 = [[rankModel.songlist objectAtIndex:1] objectForKey:@"singerName"];
    NSString *song2 = [[rankModel.songlist objectAtIndex:1] objectForKey:@"songName"];
    NSString *str2 = [NSString stringWithFormat:@"%@ - %@",singer2, song2];
    cell.numLabel2.text = str2;
    
    
    NSString *singer3 = [[rankModel.songlist objectAtIndex:2] objectForKey:@"singerName"];
    NSString *song3 = [[rankModel.songlist objectAtIndex:2] objectForKey:@"songName"];
    NSString *str3 = [NSString stringWithFormat:@"%@ - %@",singer3, song3];
    cell.numLabel3.text = str3;
    
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 给详细页面传值
    RankDetailViewController *detail =[[RankDetailViewController alloc] init];
   RankModel *rankModel = [self.array objectAtIndex:indexPath.row];
    detail.webId = rankModel.id;
    detail.pic_url = rankModel.pic_url;
    detail.details = rankModel.details;
    detail.title = rankModel.title;
    
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
    
}



@end
